<?php
/**
 * Created by PhpStorm.
 * User: gakuin
 * Date: 16/8/28
 * Time: 上午10:44
 */
namespace app\models;

use yii\base\Model;

class ResetPwdStep2Form extends Model{

    public $code;
    public $mobile;

    public function rules()
    {
        return [
            ['code', 'required','message'=>'验证码不能为空'],
            //验证输入的和表中保存的验证码
            ['code','validateCode'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'code' =>'验证码',
        ];
    }

    public function validateCode($attribute,$params){
        $tel = $this->mobile;
        $now = time()+3600*6;
        $verify = ValidateCode::find()->where(['mobile'=>$tel])->orderBy('create_time DESC')->one();

        if($verify == null){
            $this->addError($attribute,'请发送验证码');
        }else{
            $create = strtotime($verify->create_time);
            if ($create<$now-60) {

                $this->addError($attribute, '验证码过期,请重新发送验证码');
                //return;
            }elseif($verify->verify_code!=$this->code){
                $this->addError($attribute, '验证码不正确');
            }
        }
    }
}